package com.sskj.one.mine;

import android.accessibilityservice.FingerprintGestureController;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.R;
import com.sskj.one.bean.ListHuiBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class RecordHuiActivity extends BaseActivity<RecordHuiPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private String type;
    private SlimAdapter<String> slimAdapter;
    private BaseAdapter<ListHuiBean> adapter;

    private int p = 1;
    private int size = 10;


    @Override
    public int getLayoutId() {
        return R.layout.app_activity_record_hui;
    }

    @Override
    public RecordHuiPresenter getPresenter() {
        return new RecordHuiPresenter();
    }

    @Override
    public void initView() {
        type = getIntent().getStringExtra("type");
        if (type.equals(Constans.JIE_HUI)) {
            tvTitle.setText("结汇记录");
        } else if (type.equals(Constans.GOU_HUI)) {
            tvTitle.setText("购汇记录");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintColor(ContextCompat.getColor(this, R.color.divide))
                .setPaintWidth((int) RudenessScreenHelper.dp2px(this, 1))
                .setLeftPadding((int) RudenessScreenHelper.dp2px(this, 15))
                .setFirstDraw(false)
                .setLastDraw(false)
                .setRightPadding((int) RudenessScreenHelper.dp2px(this, 15)));
//        1 待审核 2到账中 3已拒绝 4已到账 5 失败
        HashMap<String, String> status = new HashMap<>();
        status.put("1", "待审核");
        status.put("2", "到账中");
        status.put("3", "已拒绝");
        status.put("4", "已到账");
        status.put("5", "失败");
        HashMap<String, String> gouhuiStatus = new HashMap<>();
        gouhuiStatus.put("1", "待支付");
        gouhuiStatus.put("2", "已支付");
        gouhuiStatus.put("3", "待审核");
        gouhuiStatus.put("4", "未通过");
        gouhuiStatus.put("5", "已通过");
        adapter = new BaseAdapter<ListHuiBean>(R.layout.item_record_hui, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, ListHuiBean item) {
                holder.setText(R.id.tv_msg, item.getMemo())
                        .setText(R.id.tv_date, item.getAddtime())
                        .setTextColor(R.id.tv_money, type.equals(Constans.JIE_HUI) ?
                                ContextCompat.getColor(App.INSTANCE, R.color.theme) : ContextCompat.getColor(App.INSTANCE, R.color.color_red))
                        .setText(R.id.tv_money, type.equals(Constans.JIE_HUI) ?
                                "-$" + item.getActual() : "+$" + item.getPrice())
                        .setText(R.id.tv_status, type.equals(Constans.JIE_HUI) ?
                                status.get(item.getState()) : gouhuiStatus.get(item.getState()));
            }
        };
    }

    @Override
    public void initData() {
        if (type.equals(Constans.JIE_HUI)) {
            mPresenter.getJieHuiRecord(true, p, size);
        } else if (type.equals(Constans.GOU_HUI)) {
            mPresenter.getGouHuiRecord(true, p, size);
        }
        smartRefreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                p = 1;
                if (type.equals(Constans.JIE_HUI)) {
                    mPresenter.getJieHuiRecord(false, p, size);
                } else if (type.equals(Constans.GOU_HUI)) {
                    mPresenter.getGouHuiRecord(false, p, size);
                }
            }
        });
        smartRefreshView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if (type.equals(Constans.JIE_HUI)) {
                    mPresenter.getJieHuiRecord(false, p, size);
                } else if (type.equals(Constans.GOU_HUI)) {
                    mPresenter.getGouHuiRecord(false, p, size);
                }
            }
        });

    }

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, RecordHuiActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public void getRecordOK(List<ListHuiBean> data) {
        if (data == null) {
            return;
        }
        if (p == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }
}
